home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / strcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-01  |  3.4 KB  |  145 lines

  1. /* This file is part of XEmacs.
  2.  
  3. XEmacs is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any
  6. later version.
  7.  
  8. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11. for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with XEmacs; see the file COPYING.  If not, write to the Free
  15. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17. /* Synched up with: Not in FSF. */
  18.  
  19. /* In SunOS 4.1.1 the strcmp and strncmp functions reference memory
  20.    past the last byte of the string! This will core dump if the memory 
  21.    following the last byte is not mapped.
  22.  
  23.    Here are correct versions by hbs@lucid.com.
  24. */
  25.  
  26. #include <string.h>
  27. #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
  28.  
  29. #define MAGIC    0x7efefeff
  30. #define HIGH_BIT_P(c) ((c) & hi_bit)
  31. #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
  32.  
  33. int
  34. strcmp (const char *x, const char *y)
  35. {
  36.   if (x == y)
  37.     return 0;
  38.   else if (ALIGNED (x) && ALIGNED (y))
  39.     {
  40.       const unsigned long *x1 = (const unsigned long *) x;
  41.       const unsigned long *y1 = (const unsigned long *) y;
  42.       unsigned long c;
  43.       unsigned long magic = MAGIC;
  44.       unsigned long not_magic = ~magic;
  45.       unsigned long hi_bit = 0x80000000;
  46.  
  47.       while ((c = *x1) == *y1)
  48.         {
  49.           if (HAS_ZERO(c)) 
  50.             {
  51.               if (!HIGH_BIT_P (c))
  52.                 return 0;
  53.               else
  54.                 {
  55.                   x = (const char *) x1;
  56.                   y = (const char *) y1;
  57.                   goto slow_loop;
  58.                 }
  59.             }
  60.               
  61.           x1++; 
  62.           y1++;
  63.         }
  64.  
  65.       x = (const char *) x1;
  66.       y = (const char *) y1;
  67.       goto slow_loop;
  68.     }
  69.   else
  70.     {
  71.       char c;
  72.  
  73.     slow_loop:
  74.  
  75.       while ((c = *x) == *y)
  76.         {
  77.           if (c == (char) 0) return 0;
  78.           x++; 
  79.           y++;
  80.         }
  81.       return (*x - *y);
  82.     }
  83. }
  84.  
  85.  
  86. int
  87. strncmp (const char *x, const char *y, size_t n)
  88. {
  89.   if ((x == y) || (n <= 0))
  90.     return 0;
  91.   else if (ALIGNED (x) && ALIGNED (y))
  92.     {
  93.       const unsigned long *x1 = (const unsigned long *) x;
  94.       const unsigned long *y1 = (const unsigned long *) y;
  95.       unsigned long c;
  96.       unsigned long magic = MAGIC;
  97.       unsigned long not_magic = ~magic;
  98.       unsigned long hi_bit = 0x80000000;
  99.  
  100.       while ((c = *x1) == *y1)
  101.         {
  102.           n -= sizeof (unsigned long);
  103.           if (n <= 0)
  104.             return 0;
  105.           
  106.           if (HAS_ZERO(c)) 
  107.             {
  108.               if (!HIGH_BIT_P (c))
  109.                 return 0;
  110.               else
  111.                 {
  112.                   x = (const char *) x1;
  113.                   y = (const char *) y1;
  114.                   goto slow_loop;
  115.                 }
  116.             }
  117.               
  118.           x1++; 
  119.           y1++;
  120.         }
  121.  
  122.       x = (const char *) x1;
  123.       y = (const char *) y1;
  124.       goto slow_loop;
  125.     }
  126.   else
  127.     {
  128.       char c;
  129.  
  130.     slow_loop:
  131.  
  132.       while ((c = *x) == *y)
  133.         {
  134.           n--;
  135.           if (n <= 0)
  136.             return 0;
  137.           if (c == (char) 0) 
  138.             return 0;
  139.           x++; 
  140.           y++;
  141.         }
  142.       return (*x - *y);
  143.     }
  144. }
  145.